home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet 2002 February / Practical Internet February 2002.iso / pc / Software / Browsing / httrack-3.09e2.exe / {app} / src / httrack.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-07  |  17.1 KB  |  551 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsshow.c console progress info                        */
  34. /* Only used on Linux version                                   */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #if HTS_WIN
  39. #else
  40. #ifndef Sleep
  41. #define Sleep(a) { if (((a)*1000)%1000000) usleep(((a)*1000)%1000000); if (((a)*1000)/1000000) sleep(((a)*1000)/1000000); }
  42. #endif
  43. #endif
  44.  
  45. #include "htsglobal.h"
  46. #include "httrack.h"
  47.  
  48. // htswrap_add
  49. #include "htswrap.h"
  50.  
  51. #if HTS_ANALYSTE_CONSOLE
  52.  
  53. /* specific definitions */
  54. #include "htsbase.h"
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <ctype.h>
  59. #ifdef _WIN32
  60. #include "Winsock.h"
  61. #endif
  62. /* END specific definitions */
  63.  
  64. // ISO VT100/220 definitions
  65. #define VT_COL_TEXT_BLACK    "30"
  66. #define VT_COL_TEXT_RED      "31"
  67. #define VT_COL_TEXT_GREEN    "32"
  68. #define VT_COL_TEXT_YELLOW   "33"
  69. #define VT_COL_TEXT_BLUE     "34"
  70. #define VT_COL_TEXT_MAGENTA  "35"
  71. #define VT_COL_TEXT_CYAN     "36"
  72. #define VT_COL_TEXT_WHITE    "37"
  73. #define VT_COL_BACK_BLACK    "40"
  74. #define VT_COL_BACK_RED      "41"
  75. #define VT_COL_BACK_GREEN    "42"
  76. #define VT_COL_BACK_YELLOW   "43"
  77. #define VT_COL_BACK_BLUE     "44"
  78. #define VT_COL_BACK_MAGENTA  "45"
  79. #define VT_COL_BACK_CYAN     "46"
  80. #define VT_COL_BACK_WHITE    "47"
  81. //
  82. #define VT_GOTOXY(X,Y)  "\33["Y";"X"f"
  83. #define VT_COLOR(C)     "\33["C"m"
  84. #define VT_RESET        "\33[m"
  85. #define VT_REVERSE      "\33[7m"
  86. #define VT_UNREVERSE    "\33[27m"
  87. #define VT_BOLD         "\33[1m"
  88. #define VT_UNBOLD       "\33[22m"
  89. #define VT_BLINK        "\33[5m"
  90. #define VT_UNBLINK      "\33[25m"
  91. //
  92. #define VT_CLREOL       "\33[K"
  93. #define VT_CLRSOL       "\33[1K"
  94. #define VT_CLRLIN       "\33[2K"
  95. #define VT_CLREOS       "\33[J"
  96. #define VT_CLRSOS       "\33[1J"
  97. #define VT_CLRSCR       "\33[2J"
  98. //
  99. #define csi(X)          printf(s_csi( X ));
  100. void vt_clear(void) {
  101.   printf("%s%s%s",VT_RESET,VT_CLRSCR,VT_GOTOXY("1","0"));
  102. }
  103. void vt_home(void) {
  104.   printf("%s%s",VT_RESET,VT_GOTOXY("1","0"));
  105. }
  106. //
  107.  
  108.  
  109. #define STYLE_STATVALUES VT_COLOR(VT_COL_TEXT_BLACK)
  110. #define STYLE_STATTEXT   VT_COLOR(VT_COL_TEXT_BLUE)   
  111. #define NStatsBuffer     14
  112. #define MAX_LEN_INPROGRESS 40
  113.  
  114. static int use_show;
  115.  
  116.  
  117. int main(int argc, char **argv) {
  118.   hts_init();
  119.  
  120.   /*
  121.   hts_htmlcheck_init         = (t_hts_htmlcheck_init)           htswrap_read("init");
  122. Log: "engine: init"
  123.  
  124.   hts_htmlcheck_uninit       = (t_hts_htmlcheck_uninit)         htswrap_read("free");
  125. Log: "engine: free"
  126.  
  127.   hts_htmlcheck_start        = (t_hts_htmlcheck_start)          htswrap_read("start");
  128. Log: "engine: start"
  129.  
  130.   hts_htmlcheck_end          = (t_hts_htmlcheck_end)            htswrap_read("end");
  131. Log: "engine: end"
  132.  
  133.   hts_htmlcheck_chopt        = (t_hts_htmlcheck_chopt)          htswrap_read("change-options");
  134. Log: "engine: change-options"
  135.  
  136.   hts_htmlcheck              = (t_hts_htmlcheck)                htswrap_read("check-html");
  137. Log: "check-html: <url>"
  138.  
  139.   hts_htmlcheck_query        = (t_hts_htmlcheck_query)          htswrap_read("query");
  140.   hts_htmlcheck_query2       = (t_hts_htmlcheck_query2)         htswrap_read("query2");
  141.   hts_htmlcheck_query3       = (t_hts_htmlcheck_query3)         htswrap_read("query3");
  142.   hts_htmlcheck_loop         = (t_hts_htmlcheck_loop)           htswrap_read("loop");
  143.   hts_htmlcheck_check        = (t_hts_htmlcheck_check)          htswrap_read("check-link");
  144. Log: none
  145.  
  146.   hts_htmlcheck_pause        = (t_hts_htmlcheck_pause)          htswrap_read("pause");
  147. Log: "pause: <lockfile>"
  148.  
  149.   hts_htmlcheck_filesave     = (t_hts_htmlcheck_filesave)       htswrap_read("save-file");
  150.   hts_htmlcheck_linkdetected = (t_hts_htmlcheck_linkdetected)   htswrap_read("link-detected");
  151. Log: none
  152.  
  153.   hts_htmlcheck_xfrstatus    = (t_hts_htmlcheck_xfrstatus)      htswrap_read("transfer-status");
  154. Log: 
  155.     "engine: transfer-status: link updated: <url> -> <file>"
  156.   | "engine: transfer-status: link recorded: <url> -> <file>"
  157.   | "engine: transfer-status: link link error (<errno>, '<err_msg>'): <url>"
  158. */
  159.   
  160.   htswrap_add("init",htsshow_init);
  161.   htswrap_add("free",htsshow_uninit);
  162.   htswrap_add("start",htsshow_start);
  163.   htswrap_add("change-options",htsshow_chopt);
  164.   htswrap_add("end",htsshow_end);
  165.   htswrap_add("check-html",htsshow_checkhtml);
  166.   htswrap_add("loop",htsshow_loop);
  167.   htswrap_add("query",htsshow_query);
  168.   htswrap_add("query2",htsshow_query2);
  169.   htswrap_add("query3",htsshow_query3);
  170.   htswrap_add("check-link",htsshow_check);
  171.   htswrap_add("pause",htsshow_pause);
  172.   htswrap_add("save-file",htsshow_filesave);
  173.   htswrap_add("link-detected",htsshow_linkdetected);
  174.   htswrap_add("transfer-status",htsshow_xfrstatus);
  175.  
  176.   return hts_main(argc,argv);
  177. }
  178.  
  179.  
  180. /* CALLBACK FUNCTIONS */
  181.  
  182. /* Initialize the Winsock */
  183. void __cdecl htsshow_init(void) {
  184. #ifdef _WIN32
  185.   {
  186.     WORD   wVersionRequested;   // requested version WinSock API
  187.     WSADATA wsadata;            // Windows Sockets API data
  188.     int stat;
  189.     wVersionRequested = 0x0101;
  190.     stat = WSAStartup( wVersionRequested, &wsadata );
  191.     if (stat != 0) {
  192.       printf("Winsock not found!\n");
  193.       return;
  194.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  195.       printf("WINSOCK.DLL does not support version 1.1\n");
  196.       WSACleanup();
  197.       return;
  198.     }
  199.   }
  200. #endif
  201.  
  202. }
  203. void __cdecl htsshow_uninit(void) {
  204. #ifdef _WIN32
  205.   WSACleanup();
  206. #endif
  207. }
  208. int __cdecl htsshow_start(httrackp* opt) {
  209.   use_show=0;
  210.   if (opt->verbosedisplay==2) {
  211.     use_show=1;
  212.     vt_clear();
  213.   }
  214.   return 1; 
  215. }
  216. int __cdecl htsshow_chopt(httrackp* opt) {
  217.   return __cdecl htsshow_start(opt);
  218. }
  219. int  __cdecl htsshow_end(void) { 
  220.   return 1; 
  221. }
  222. int __cdecl htsshow_checkhtml(char* html,int len,char* url_adresse,char* url_fichier) {
  223.   return 1;
  224. }
  225. int __cdecl htsshow_loop(lien_back* back,int back_max,int back_index,int lien_n,int lien_tot,int stat_time, hts_stat_struct* stats) {    // appelΘ α chaque boucle de HTTrack
  226.   static TStamp prev_mytime=0;
  227.   static t_InpInfo SInfo;
  228.   //
  229.   TStamp mytime;
  230.   long int rate=0;
  231.   char st[256];
  232.   //
  233.   int stat_written=-1;
  234.   int stat_updated=-1;
  235.   int stat_errors=-1;
  236.   int nbk=-1;
  237.   LLint nb=-1;
  238.   int stat_nsocket=-1;
  239.   LLint stat_bytes=-1;
  240.   LLint stat_bytes_recv=-1;
  241.   int irate=-1;
  242.   if (stats) {
  243.     stat_written=stats->stat_files;
  244.     stat_updated=stats->stat_updated_files;
  245.     stat_errors=stats->stat_errors;
  246.     nbk=stats->nbk;
  247.     stat_nsocket=stats->stat_nsocket;
  248.     irate=(int)stats->rate;
  249.     nb=stats->nb;
  250.     stat_bytes=stats->nb;
  251.     stat_bytes_recv=stats->HTS_TOTAL_RECV;
  252.   }
  253.  
  254.   if (!use_show)
  255.     return 1;
  256.  
  257.   mytime=mtime_local();
  258.   if ((stat_time>0) && (stat_bytes_recv>0))
  259.     rate=(int)(stat_bytes_recv/stat_time);
  260.   else
  261.     rate=0;    // pas d'infos
  262.  
  263.   /* Infos */
  264.   if (stat_bytes>=0) SInfo.stat_bytes=stat_bytes;      // bytes
  265.   if (stat_time>=0) SInfo.stat_time=stat_time;         // time
  266.   if (lien_tot>=0) SInfo.lien_tot=lien_tot; // nb liens
  267.   if (lien_n>=0) SInfo.lien_n=lien_n;       // scanned
  268.   SInfo.stat_nsocket=stat_nsocket;          // socks
  269.   if (rate>0)  SInfo.rate=rate;                // rate
  270.   if (irate>=0) SInfo.irate=irate;             // irate
  271.   if (SInfo.irate<0) SInfo.irate=SInfo.rate;
  272.   if (SInfo.stat_back>=0) SInfo.stat_back=nbk;
  273.   if (stat_written>=0) SInfo.stat_written=stat_written;
  274.   if (stat_updated>=0) SInfo.stat_updated=stat_updated;
  275.   if (stat_errors>=0)  SInfo.stat_errors=stat_errors;
  276.  
  277.  
  278.   if ( ((mytime - prev_mytime)>100) || ((mytime - prev_mytime)<0) ) {
  279.     prev_mytime=mytime;
  280.  
  281.     
  282.     st[0]='\0';
  283.     qsec2str(st,stat_time);
  284.     vt_home();
  285.     printf(
  286.     VT_GOTOXY("1","1")
  287.     VT_CLREOL
  288.                            STYLE_STATTEXT   "Bytes saved:"
  289.                            STYLE_STATVALUES " \t%s"
  290.                            "\t"
  291.     VT_CLREOL
  292.     VT_GOTOXY("40","1")
  293.                            STYLE_STATTEXT   "Links scanned:"
  294.                            STYLE_STATVALUES " \t%d/%d (+%d)"
  295.     VT_CLREOL"\n"VT_CLREOL
  296.     VT_GOTOXY("1","2")
  297.                            STYLE_STATTEXT   "Time:"
  298.                                             " \t"
  299.                            STYLE_STATVALUES "%s"
  300.                            "\t"
  301.     VT_CLREOL
  302.     VT_GOTOXY("40","2")
  303.                            STYLE_STATTEXT   "Files written:"
  304.                                             " \t"
  305.                            STYLE_STATVALUES "%d"
  306.     VT_CLREOL"\n"VT_CLREOL
  307.     VT_GOTOXY("1","3")
  308.                            STYLE_STATTEXT   "Transfer rate:"
  309.                                             " \t"
  310.                            STYLE_STATVALUES "%s (%s)"
  311.                            "\t"
  312.     VT_CLREOL
  313.     VT_GOTOXY("40","3")
  314.                            STYLE_STATTEXT   "Files updated:"
  315.                                             " \t"
  316.                            STYLE_STATVALUES "%d"
  317.     VT_CLREOL"\n"VT_CLREOL
  318.     VT_GOTOXY("1","4")
  319.                            STYLE_STATTEXT   "Active connections:"
  320.                                             " \t"
  321.                            STYLE_STATVALUES "%d"
  322.                            "\t"
  323.     VT_CLREOL
  324.     VT_GOTOXY("40","4")
  325.                            STYLE_STATTEXT   "Errors:"
  326.                            STYLE_STATVALUES " \t"
  327.                            STYLE_STATVALUES "%d"
  328.     VT_CLREOL"\n",
  329.       /* */
  330.       (char*)int2bytes(SInfo.stat_bytes),
  331.       (int)lien_n,(int)SInfo.lien_tot,(int)nbk,
  332.       (char*)st,
  333.       (int)SInfo.stat_written,
  334.       (char*)int2bytessec(SInfo.irate),(char*)int2bytessec(SInfo.rate),
  335.       (int)SInfo.stat_updated,
  336.       (int)SInfo.stat_nsocket,
  337.       (int)SInfo.stat_errors
  338.       /* */
  339.       );
  340.  
  341.     
  342.     // parcourir registre des liens
  343.     if (back_index>=0) {  // seulement si index passΘ
  344.       int j,k;
  345.       int index=0;
  346.       int ok=0;         // idem
  347.       int l;            // idem
  348.       //
  349.       t_StatsBuffer StatsBuffer[NStatsBuffer];
  350.       
  351.       {
  352.         int i;
  353.         for(i=0;i<NStatsBuffer;i++) {
  354.           strcpy(StatsBuffer[i].state,"");
  355.           strcpy(StatsBuffer[i].name,"");
  356.           strcpy(StatsBuffer[i].file,"");
  357.           strcpy(StatsBuffer[i].url_sav,"");
  358.           StatsBuffer[i].back=0;
  359.           StatsBuffer[i].size=0;
  360.           StatsBuffer[i].sizetot=0;
  361.         }
  362.       }
  363.       for(k=0;k<2;k++) {    // 0: lien en cours 1: autres liens
  364.         for(j=0;(j<3) && (index<NStatsBuffer);j++) {  // passe de prioritΘ
  365.           int _i;
  366.           for(_i=0+k;(_i< max(back_max*k,1) ) && (index<NStatsBuffer);_i++) {  // no lien
  367.             int i=(back_index+_i)%back_max;    // commencer par le "premier" (l'actuel)
  368.             if (back[i].status>=0) {     // signifie "lien actif"
  369.               // int ok=0;  // OPTI
  370.               ok=0;
  371.               switch(j) {
  372.               case 0:     // prioritaire
  373.                 if ((back[i].status>0) && (back[i].status<99)) {
  374.                   strcpy(StatsBuffer[index].state,"receive"); ok=1;
  375.                 }
  376.                 break;
  377.               case 1:
  378.                 if (back[i].status==99) {
  379.                   strcpy(StatsBuffer[index].state,"request"); ok=1;
  380.                 }
  381.                 else if (back[i].status==100) {
  382.                   strcpy(StatsBuffer[index].state,"connect"); ok=1;
  383.                 }
  384.                 else if (back[i].status==101) {
  385.                   strcpy(StatsBuffer[index].state,"search"); ok=1;
  386.                 }
  387.                 else if (back[i].status==1000) {    // ohh le beau ftp
  388.                   sprintf(StatsBuffer[index].state,"ftp: %s",back[i].info); ok=1;
  389.                 }
  390.                 break;
  391.               default:
  392.                 if (back[i].status==0) {  // prΩt
  393.                   if ((back[i].r.statuscode==200)) {
  394.                     strcpy(StatsBuffer[index].state,"ready"); ok=1;
  395.                   }
  396.                   else if ((back[i].r.statuscode>=100) && (back[i].r.statuscode<=599)) {
  397.                     char tempo[256]; tempo[0]='\0';
  398.                     infostatuscode(tempo,back[i].r.statuscode);
  399.                     strcpy(StatsBuffer[index].state,tempo); ok=1;
  400.                   }
  401.                   else {
  402.                     strcpy(StatsBuffer[index].state,"error"); ok=1;
  403.                   }
  404.                 }
  405.                 break;
  406.               }
  407.               
  408.               if (ok) {
  409.                 char s[HTS_URLMAXSIZE*2];
  410.                 //
  411.                 StatsBuffer[index].back=i;        // index pour + d'infos
  412.                 //
  413.                 s[0]='\0';
  414.                 strcpy(StatsBuffer[index].url_sav,back[i].url_sav);   // pour cancel
  415.                 if (strcmp(back[i].url_adr,"file://"))
  416.                   strcat(s,back[i].url_adr);
  417.                 else
  418.                   strcat(s,"localhost");
  419.                 if (back[i].url_fil[0]!='/')
  420.                   strcat(s,"/");
  421.                 strcat(s,back[i].url_fil);
  422.                 
  423.                 StatsBuffer[index].file[0]='\0';
  424.                 {
  425.                   char* a=strrchr(s,'/');
  426.                   if (a) {
  427.                     strncat(StatsBuffer[index].file,a,200);
  428.                     *a='\0';
  429.                   }
  430.                 }
  431.                 
  432.                 if ((l=strlen(s))<MAX_LEN_INPROGRESS)
  433.                   strcpy(StatsBuffer[index].name,s);
  434.                 else {
  435.                   // couper
  436.                   StatsBuffer[index].name[0]='\0';
  437.                   strncat(StatsBuffer[index].name,s,MAX_LEN_INPROGRESS/2-2);
  438.                   strcat(StatsBuffer[index].name,"...");
  439.                   strcat(StatsBuffer[index].name,s+l-MAX_LEN_INPROGRESS/2+2);
  440.                 }
  441.                                
  442.                 if (back[i].r.totalsize>0) {  // taille prΘdΘfinie
  443.                   StatsBuffer[index].sizetot=back[i].r.totalsize;
  444.                   StatsBuffer[index].size=back[i].r.size;
  445.                 } else {  // pas de taille prΘdΘfinie
  446.                   if (back[i].status==0) {  // prΩt
  447.                     StatsBuffer[index].sizetot=back[i].r.size;
  448.                     StatsBuffer[index].size=back[i].r.size;
  449.                   } else {
  450.                     StatsBuffer[index].sizetot=8192;
  451.                     StatsBuffer[index].size=(back[i].r.size % 8192);
  452.                   }
  453.                 }
  454.                 index++;
  455.               }
  456.             }
  457.           }
  458.         }
  459.       }
  460.  
  461.       /* LF */
  462.       printf("%s\n",VT_CLREOL);
  463.  
  464.       /* Display current job */
  465.       {
  466.         int parsing=0;
  467.         printf("Current job: ");
  468.         if (!(parsing=hts_is_parsing(-1)))
  469.           printf("receiving files");
  470.         else {
  471.           switch(hts_is_testing()) {
  472.           case 0:
  473.             printf("parsing HTML file (%d%%)",parsing);
  474.             break;
  475.           case 1:
  476.             printf("parsing HTML file: testing links (%d%%)",parsing);
  477.             break;
  478.           case 2:
  479.             printf("purging files");
  480.             break;
  481.           }
  482.         }
  483.         printf("%s\n",VT_CLREOL);
  484.       }
  485.       
  486.       /* Display background jobs */
  487.       {
  488.         int i;
  489.         for(i=0;i<NStatsBuffer;i++) {
  490.           if (strnotempty(StatsBuffer[i].state)) {
  491.             printf(VT_CLREOL" %s - \t%s%s \t%s / \t%s",
  492.               StatsBuffer[i].state,
  493.               StatsBuffer[i].name,
  494.               StatsBuffer[i].file,
  495.               int2bytes(StatsBuffer[i].size),
  496.               int2bytes(StatsBuffer[i].sizetot)
  497.               );
  498.           }
  499.           printf("%s\n",VT_CLREOL);
  500.         }
  501.       }
  502.  
  503.  
  504.     }
  505.  
  506.   }
  507.  
  508.  
  509.  
  510.   return 1;
  511. }
  512. char* __cdecl htsshow_query(char* question) {
  513.   static char s[12]="";
  514.   printf("%s\nPress <Y><Enter> to confirm, <N><Enter> to abort\n",question);
  515.   io_flush; linput(stdin,s,4);
  516.   return s;
  517. }
  518. char* __cdecl htsshow_query2(char* question) {
  519.   static char s[12]="";
  520.   printf("%s\nPress <Y><Enter> to confirm, <N><Enter> to abort\n",question);
  521.   io_flush; linput(stdin,s,4);
  522.   return s;
  523. }
  524. char* __cdecl htsshow_query3(char* question) {
  525.   static char line[256];
  526.   do {
  527.     io_flush; linput(stdin,line,206);
  528.   } while(!strnotempty(line));
  529.   printf("ok..\n");
  530.   return line;
  531. }
  532. int __cdecl htsshow_check(char* adr,char* fil,int status) {
  533.   return -1;
  534. }
  535. void __cdecl htsshow_pause(char* lockfile) {
  536.   while (fexist(lockfile)) {
  537.     Sleep(1000);
  538.   }
  539. }
  540. void __cdecl htsshow_filesave(char* file) {
  541. }
  542. int __cdecl htsshow_linkdetected(char* link) {
  543.   return 1;
  544. }
  545. int __cdecl htsshow_xfrstatus(lien_back* back) {
  546.   return 1;
  547. }
  548.  
  549.  
  550. #endif
  551.